package com.jimet.property;

import java.sql.ResultSet;
import java.util.Vector;

import com.jimet.db.JimetPropertyDB;

public class JimetProperties {

	protected String TBL_PREFIX;
	protected int pos;
	public Vector<JimetProperty> propVec;

	public JimetProperties(String prefix) 
	{
		this.TBL_PREFIX = prefix;
	}

	public int size()
	{
		if(this.propVec == null)
			return 0;
		else
			return this.propVec.size();
	}
	
	public void loadPropsByID(String[] strArrPropsId)  throws Exception 
	{
		this.pos = -1;
		this.propVec = new Vector<JimetProperty>();
		JimetPropertyDB jimdb = new JimetPropertyDB();	
		ResultSet rs = jimdb.getPropsByID(strArrPropsId,this.TBL_PREFIX);
		while(rs.next())
		{
			JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
			jprop.setId(rs.getInt(1));
			jprop.setName(rs.getString(2));
			jprop.setType(rs.getString(3));
			this.propVec.add(jprop);
		}
		if(this.propVec == null)
		{
			JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
			jprop.setId(0);
			jprop.setName("0");
			jprop.setType("0");
			this.propVec.add(jprop);
		}
		jimdb.disconnect();
	}
	
	
	  public void search(String strSort, String strLike, String strSortBy,String strSearchBy) throws Exception
		{
			this.pos = -1;
			this.propVec = new Vector<JimetProperty>();
			JimetPropertyDB jimdb = new JimetPropertyDB();		
			ResultSet rs = jimdb.getPropSearchResults(strSort,strLike,strSortBy,strSearchBy,this.TBL_PREFIX);
			while(rs.next())
			{
				JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
				jprop.setId(rs.getInt(1));
				jprop.setName(rs.getString(2));
				jprop.setType(rs.getString(3));
				this.propVec.add(jprop);

			}
			
			if(this.propVec == null)
			{
				JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
				jprop.setId(0);
				jprop.setName("0");
				jprop.setType("0");
				this.propVec.add(jprop);
			}

			
			jimdb.disconnect();
		}
	  
	public void loadObjPropsByID(String[] strArrPropsId, String objId)  throws Exception 
	{
		this.pos = -1;
		this.propVec = new Vector<JimetProperty>();
		JimetPropertyDB jimdb = new JimetPropertyDB();	
		ResultSet rs = jimdb.getObjPropsByID(strArrPropsId,this.TBL_PREFIX,objId);
		while(rs.next())
		{
			JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
			jprop.setName(rs.getString(1));
			jprop.setValue(rs.getString(2));
			jprop.setId(rs.getInt(3));
			this.propVec.add(jprop);
		}
		
		if(this.propVec == null)
		{
			JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
			jprop.setId(0);
			jprop.setName("0");
			jprop.setType("0");
			this.propVec.add(jprop);
		}
		jimdb.disconnect();
	}

	public void delete() throws Exception
	{
		for(int i=0;i<propVec.size();i++)
		{
			JimetProperty jprop = (JimetProperty) propVec.elementAt(i);
			jprop.delete();
		}
	}
	
	public void deleteUserProps(String userID) throws Exception
	{
		JimetPropertyDB jimdb = new JimetPropertyDB();	
		jimdb.deleteUserProps(userID);
	 	
		jimdb.disconnect();
	}
	
	public void add(JimetProperty jprop)
	{

		if(this.propVec == null)
		{
			this.pos = -1;
			this.propVec = new Vector<JimetProperty>();
		}
		this.propVec.add(jprop);
	}
	
	
	public void reset()
	{
		this.pos = -1;
	}

	public boolean next()
	{
		if(this.propVec == null)
			return false;
		pos++;
		return  (pos < this.propVec.size());
	}

	public JimetProperty getNext()
	{
		return (JimetProperty) this.propVec.elementAt(this.pos);
	}

	public void load()  throws Exception 
	{
		this.pos = -1;
		this.propVec = new Vector<JimetProperty>();
		JimetPropertyDB jimdb = new JimetPropertyDB();	
		ResultSet rs = jimdb.getAllProperties(this.TBL_PREFIX);
		while(rs.next())
		{
			JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
			jprop.setId(rs.getInt(1));
			jprop.setName(rs.getString(2));
			jprop.setType(rs.getString(3));
			this.propVec.add(jprop);
		}	
		
		if(this.propVec == null)
		{
			JimetProperty jprop = new JimetProperty(this.TBL_PREFIX);
			jprop.setId(0);
			jprop.setName("0");
			jprop.setType("0");
			this.propVec.add(jprop);
		}
		jimdb.disconnect();
	}

	public void remove(String propName)
	{
	
		if(this.propVec == null)
		{
			this.reset();
			while(this.next()) 
			{
				JimetProperty jprop = this.getNext();
				if(jprop.getName().equals(propName)) 
				{
					this.propVec.remove(jprop);
				}
			}
		}
		this.reset();
	
	}

	public boolean setPropValueByName(String propName, String value)
	{
		if(this.propVec != null) 
		{
		this.pos = -1;
	
			for(int i=0; i < this.propVec.size(); i++) 
			{
				JimetProperty jprop = (JimetUserProperty) this.propVec.elementAt(i);
				
				if(jprop.getName().equals(propName)) 
				{
					jprop.setValue(value);
					return true;
				}
			
			}
		}
		this.pos = -1;
		return false;
	}

	public String getPropValueByName(String propName)
	{
		if(this.propVec != null) 
		{
		this.pos = -1;
	
			for(int i=0; i < this.propVec.size(); i++) 
			{
				JimetProperty jprop = (JimetProperty) this.propVec.elementAt(i);
				
				if(jprop.getName().equals(propName)) 
				{
					return jprop.getValue();
				}
			
			}
		}
		this.pos = -1;
		return "";
	}

	/**
	 * @return Returns the tBL_PREFIX.
	 */
	public String getTBL_PREFIX() {
		return TBL_PREFIX;
	}

	/**
	 * @param tbl_prefix The tBL_PREFIX to set.
	 */
	public void setTBL_PREFIX(String tbl_prefix) {
		TBL_PREFIX = tbl_prefix;
	}
	

}
